package com.google.code.dajly.model;


import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;

import junit.framework.TestCase;

public class FeedTest extends TestCase {
	
	private Feed _f;
	private String url= "http://dajly.googlecode.com/files/corriere.xml";

	protected void setUp() throws Exception {
		super.setUp();
		_f = new Feed("http://dajly.googlecode.com/files/corriere.xml");
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testRefreshFeed() {
		try {
			_f.refreshFeed();
		} catch (MalformedURLException e) {
			fail("Malformed");
		} catch (IOException e) {
			fail("IO");
		}
	}

	public void testSetFilter() {
		_f.setFilter(new FilterType().setFilter("Rifiuti").setType("title"));
		for(Entry _e : _f.getEntries()){
			assertTrue(_e.getTitle().contains("Rifiuti"));
		}
	}

	public void testFeed() {
		try {
			_f = new Feed("http://dajly.googlecode.com/files/corriere.xml");
		} catch (MalformedURLException e) {
			fail("malf");
		} catch (IOException e) {
			fail("io");
		}
		
		assertNotNull(_f);
		
	}

	public void testGetTitle() {
		SyndFeedInput _input = new SyndFeedInput();
		SyndFeed _feed = null;
		try {
			_feed = _input.build(new InputStreamReader(new URL(this.url).openStream()));
		} catch (Exception e) {
			fail("exception");
		}
		
		assertEquals(_f.getTitle(),_feed.getTitle());
		
	}

	public void testGetDescription() {
		SyndFeedInput _input = new SyndFeedInput();
		SyndFeed _feed = null;
		try {
			_feed = _input.build(new InputStreamReader(new URL(this.url).openStream()));
		} catch (Exception e) {
			fail("exception");
		}
		
		assertEquals(_f.getDescription(),_feed.getDescription());
	}

	public void testGetURL() {
		
		assertEquals(_f.getURL().toExternalForm(), this.url);
	}

	public void testGetIconURL() {
		SyndFeedInput _input = new SyndFeedInput();
		SyndFeed _feed = null;
		try {
			_feed = _input.build(new InputStreamReader(new URL(this.url).openStream()));
		} catch (Exception e) {
			fail("exception");
		}
		
		assertEquals(_f.getIconURL().toExternalForm(),_feed.getImage().getUrl());
	}

	public void testGetEntries() {
		SyndFeedInput _input = new SyndFeedInput();
		SyndFeed _feed = null;
		try {
			_feed = _input.build(new InputStreamReader(new URL(this.url).openStream()));
		} catch (Exception e) {
			fail("exception");
		}
		
		assertEquals(_f.getEntries().size(), _feed.getEntries().size());
		List<?> _entries = _feed.getEntries();
		for(int i = 0 ;  i < _f.getEntries().size() ; i++){
			SyndEntryImpl _e =((SyndEntryImpl)_entries.get(i));
			
			assertEquals(_f.getEntries().get(i).getTitle(), _e.getTitle());
			assertEquals(_f.getEntries().get(i).getMessage(), _e.getDescription().getValue());
			assertEquals(_f.getEntries().get(i).getAuthor(), _e.getAuthor());
			
		}
	}

	public void testContains() {
		assertTrue(_f.contains(_f.getEntries().get(0)));
		Entry _entry = null;
		try {
			_entry = new Entry("titolo", "descrizione", "autore", new URL("http://www.google.com"), new Date());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertFalse(_f.contains(_entry));
	}

	public void testGetLocations() {
		ArrayList<String> locations = null;
        SAXBuilder builder = new SAXBuilder();
		try {
			URL _url;
			_url = new URL(url);
			Document doc;
			doc = builder.build(_url.openStream());
			Namespace ns = Namespace.getNamespace("dajly", 
			"http://code.google.com/p/dajly/wiki/namespace");
			
			// Getting Locations
			try {
				String _locs = doc.getRootElement().getChild("channel").getChild("location", ns).getText();

				if (_locs != null){
					locations = new ArrayList<String>();
					for(String _location : _locs.split(";"))
						locations.add(_location);
				}
			}catch(NullPointerException e){
				System.out.println(this + ": no custom location");
			}

		
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e){}
		
		
		if(_f.hasLocation()){
			assertEquals(_f.getLocations().size(), locations.size());
			
			for(int i = 0 ; i < _f.getLocations().size() ; i++)
				assertEquals(_f.getLocations().get(i), locations.get(i));
			
		}
		
		
	}

	public void testGetBusinesses() {
		ArrayList<String> businesses=null;
        SAXBuilder builder = new SAXBuilder();
		try {
			URL _url;
			_url = new URL(url);
			Document doc = builder.build(_url.openStream());
			Namespace ns = Namespace.getNamespace("dajly", 
			"http://code.google.com/p/dajly/wiki/namespace");
			
			// Getting Businesses
			try {
				String _buss = doc.getRootElement().getChild("channel").getChild("business", ns).getText();

				if (_buss != null){
					businesses = new ArrayList<String>();
					for(String _business : _buss.split(";"))
						businesses.add(_business);
				}
			}catch(NullPointerException e){
				System.out.println(this + ": no custom business");
			}
	
		
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		
		if(_f.hasBusiness()){
			assertEquals(_f.getBusinesses().size(), businesses.size());
			
			for(int i = 0 ; i < _f.getBusinesses().size() ; i++)
				assertEquals(_f.getBusinesses().get(i), businesses.get(i));
			
		}
		
	}

}
